<?php

class Application_Model_Printers {

    protected $printersDbTable;
    protected $cartridgesDbTable;
    
    public function __construct() {
	$this->printersDbTable = new Application_Model_DbTable_Printers;
	$this->cartridgesDbTable = new Application_Model_DbTable_Cartridges;
    }

    public function savePrinter(Application_Model_Printer $printer){

    }
    
    public function saveCartridge(Application_Model_Cartridge $cartridge){
	$newCartridge = array(
	    'name' => $cartridge->getName(),
	    'stock' => $cartridge->getStock(),
	    'color' => $cartridge->getColor()
	);
	if($cartridge->getId()===null){
	      $this->cartridgesDbTable->insert($newCartridge);
	}else{
	    $this->cartridgesDbTable->update($newCartridge, array('id = ?' => $cartridge->getId()));
	}
    }
    
    public function listCartridges(){
	$result = $this->cartridgesDbTable->fetchAll();
	$cartrigesList = array();
	foreach ($result as $res){
	    $cart = new Application_Model_Cartridge;
	    $cart->setId($res->id)
		    ->setName($res->name)
		    ->setStock($res->stock)
		    ->setColor($res->color);
	    $cartrigesList[] = $cart;
	}
	return $cartrigesList;
    }
    
    
    
    public function getColors(){
	return array(
	    'Black',
	    'Cyan',
	    'Magenta',
	    'Yellow'
	);
    }
    
    public function getColorById($id){
	$colors = $this->getColors();
	return $colors[$id];
    }
    




}

